package models

import (
	"go-admin/global/orm"
	"go-admin/tools"
	"time"
	_ "time"
)

type ZxBuild struct {
	Id         int       `json:"id" gorm:"type:int(11) unsigned;primary_key"`       //
	BuilderId  int       `json:"builderId,omitempty" gorm:"type:int(11) unsigned;"` // 施工者id
	BuildCost  int       `json:"buildCost,omitempty" gorm:"type:int(10);"`          // 施工成本
	BuildState string    `json:"buildState,omitempty"`
	BuildImgs  string    `json:"buildImgs" gorm:"type:varchar(200);"` // 施工图
	FinishDate time.Time `json:"finishDate" gorm:"type:timestamp;"`   // 完成日期
	Note       string    `json:"note" gorm:"type:varchar(200);"`      // 备注
	CreateBy   string    `json:"createBy" gorm:"type:varchar(128);"`  // 创建者
	UpdateBy   string    `json:"updateBy" gorm:"type:varchar(128);"`  // 更新者
	DataScope  string    `json:"dataScope" gorm:"-"`
	Params     string    `json:"params"  gorm:"-"`
	BaseModel
}

func (ZxBuild) TableName() string {
	return "zx_build"
}

// 创建ZxBuild
func (e *ZxBuild) Create() (ZxBuild, error) {
	var doc ZxBuild
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// 获取ZxBuild
func (e *ZxBuild) Get() (ZxBuild, error) {
	var doc ZxBuild
	table := orm.Eloquent.Table(e.TableName())

	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}

	//if e.OrderId != 0 {
	//	table = table.Where("order_id = ?", e.OrderId)
	//}

	//if e.CustomId != 0 {
	//	table = table.Where("custom_id = ?", e.CustomId)
	//}

	//if e.Builder != "" {
	//	table = table.Where("builder like ?", "%"+e.Builder+"%")
	//}

	//if e.FinishState != "" {
	//	table = table.Where("finish_state = ?", e.FinishState)
	//}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

func (e *ZxBuild) Select() (ZxBuild, error) {
	var doc ZxBuild
	table := orm.Eloquent.Table(e.TableName())

	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}

	//if e.OrderId != 0 {
	//	table = table.Where("order_id = ?", e.OrderId)
	//}

	//if e.CustomId != 0 {
	//	table = table.Where("custom_id = ?", e.CustomId)
	//}

	//if e.Builder != "" {
	//	table = table.Where("builder like ?", "%"+e.Builder+"%")
	//}

	//if e.FinishState != "" {
	//	table = table.Where("finish_state = ?", e.FinishState)
	//}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取ZxBuild带分页
func (e *ZxBuild) GetPage(pageSize int, pageIndex int) ([]ZxBuild, int, error) {
	var doc []ZxBuild

	table := orm.Eloquent.Select("*").Table(e.TableName())

	//if e.OrderId != 0 {
	//	table = table.Where("order_id = ?", e.OrderId)
	//}

	//if e.CustomId != 0 {
	//	table = table.Where("custom_id = ?", e.CustomId)
	//}

	//if e.Builder != "" {
	//	table = table.Where("builder like ?", "%"+e.Builder+"%")
	//}

	//if e.FinishState != "" {
	//	table = table.Where("finish_state = ?", e.FinishState)
	//}

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新ZxBuild
func (e *ZxBuild) Update() (update ZxBuild, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", e.Id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除ZxBuild
func (e *ZxBuild) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).Delete(&ZxBuild{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *ZxBuild) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id in (?)", id).Delete(&ZxBuild{}).Error; err != nil {
		return
	}
	Result = true
	return
}
